<template>
	<view style="height: 100%;">
		<cu-custom :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">{{navTitle}}</block>
		</cu-custom>
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :up="upOption">
			<view v-for="(item,index) in dataList" :key="index">
				<post-item @visibleChange="visibleChangeAction(item,index)" @clickTopic="clickTopicAction(item)" @delete="deletePostAction(item)" @edit="jumpToEditPost(item)" :showDeleteBtn="showEditBtn"
					:showEditBtn="showEditBtn" :showAuditStatus="showAuditStatus" @click="jumpDetail(item)" :post="item"
					:isLast="index===dataList.length-1"></post-item>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import PostItem from '@/components/post-item.vue';
	import {
		getTimeDistance
	} from '@/api/utils.js'
	import {
		getUserFavoriteList,
		getOtherUserPostList,
		getUserPostList,
		getOtherUserFavoriteList,
		deletePost,
		updateOnlySelfVisible
	} from '@/api/api.js'
	import {
		isRead
	} from '@/api/statusCache.js'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import config from '@/api/config.js'
	export default {
		components: {
			PostItem
		},
		mixins: [MescrollMixin],
		name: "UserPost",
		computed: {
			listTop() {
				return 35 * (this.windowHeight / 568)
			}
		},
		onLoad(options) {
			if (options.userId !== null && options.userId !== undefined && options.userId.length > 0) {
				this.hostUserId = options.userId
				this.isVisitMode = true
				console.log('isVisit:' + this.isVisitMode)
			}
			if (options.isFav !== null && options.isFav !== undefined && options.isFav.length > 0) {
				this.isFavMode = true
				console.log('isFav:' + this.isFavMode)
			}
		},
		onPageScroll() {
			uni.$emit('UserPostListDidScroll')
		},
		data() {
			return {
				isFavMode: false,
				hostUserId: null,
				isVisitMode: false,
				dataList: [],
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					textNoMore: config.customNoMore,
					noMoreSize: 1, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
			}
		},
		computed: {
			showEditBtn() {
				return this.isFavMode == false && this.hostUserId == null
			},
			showAuditStatus() {
				return this.isFavMode == false && this.hostUserId == null
			},
			navTitle() {
				if (this.isFavMode) {
					if (this.isVisitMode) {
						return '他的收藏'
					}
					return '我的收藏'
				} else {
					if (this.isVisitMode) {
						return '他的帖子'
					}
					return '我的帖子'
				}
			}
		},
		methods: {
			realUpdateVisible(item,index) 
			{
				let status = item.only_self_visible==1 ? 0:1
				uni.showLoading({
					title:'正在设置...'
				})
				updateOnlySelfVisible(item.post_id,status).then(res=>{
					uni.hideLoading()
					this.dataList[index].only_self_visible = status
					uni.showToast({
						title:'设置成功!'
					})
				}).catch(error=>{
					uni.hideLoading()
				})
			},
			visibleChangeAction(item,index)
			{
				if(item.only_self_visible == 0) {
					let that = this
					uni.showModal({
						title:'设置提示',
						content:'隐藏后将只有自己能够看到此帖，是否继续?',
						confirmText:'确认隐藏',
						cancelText:'手滑了',
						success(res) {
							if(res.confirm) {
								that.realUpdateVisible(item,index)
							}
						}
					})
				}else{
					this.realUpdateVisible(item,index)
				}
			},
			clickTopicAction(item)
			{
				console.log('topic is '+item.topic.title);
				uni.navigateTo({
					url:'/pages/always/topic-post-list/topic-post-list?topicId='+item.topic.topic_id,
					success: function(res) {
						res.eventChannel.emit('topicPostList',item.topic)
					}
				})
			},
			deletePostRequest(postId) 
			{
				uni.showLoading({
					title: '正在删除...'
				})
				deletePost(postId).then(res => {
					uni.hideLoading()
					uni.showToast({
						title: '删除成功'
					})
					if (this.mescroll) {
						this.mescroll.resetUpScroll()
					}
				}).catch(error => {
					uni.hideLoading()
					uni.showToast({
						icon: 'none',
						title: '删除失败'
					})
				})
			},
			deletePostAction(post) {
				let that = this
				uni.showModal({
					title: '删除帖子提示',
					content: '帖子删除后将无法找回，确认需要删除?',
					confirmText: '确认删除',
					cancelText: '手滑了',
					success(res) {
						if (res.confirm) {
							that.deletePostRequest(post.post_id)
						}
					}
				})
			},
			jumpToEditPost(post) {
				uni.navigateTo({
					url: '/pages/publish/publish?postId=' + post.post_id
				})
			},
			jumpDetail(post) {
				uni.navigateTo({
					url: '/pages/detail/detail?postId=' + post.post_id
				})
			},
			getDataList(pageIndex, pageSize) {
				if (this.isFavMode) {
					if (this.isVisitMode) {
						getOtherUserFavoriteList(this.hostUserId, pageIndex, pageSize).then(res => {
							res.data.list.forEach(item => {
								if (item.image_list && item.image_list.length > 0) {
									item.icon = 'pic'
								}
								item.show_time = getTimeDistance(item.created_at)
							})
							let pageList = res.data.list
							if (pageIndex == 0) {
								this.dataList = pageList
							} else {
								this.dataList = this.dataList.concat(pageList);
							}
							console.log('page length:' + pageList.length)
							let hasNext = pageList.length >= pageSize
							if (this.mescroll) {
								this.mescroll.endSuccess(pageList.length, hasNext);
							}
						}).catch(error => {
							if (this.mescroll) {
								this.mescroll.endErr()
							}
						})
					} else {
						getUserFavoriteList(pageIndex, pageSize).then(res => {
							res.data.list.forEach(item => {
								if (item.image_list && item.image_list.length > 0) {
									item.icon = 'pic'
								}
								item.show_time = getTimeDistance(item.created_at)
							})
							let pageList = res.data.list
							if (pageIndex == 0) {
								this.dataList = pageList
							} else {
								this.dataList = this.dataList.concat(pageList);
							}
							console.log('page length:' + pageList.length)
							let hasNext = pageList.length >= pageSize
							if (this.mescroll) {
								this.mescroll.endSuccess(pageList.length, hasNext);
							}
						}).catch(error => {
							if (this.mescroll) {
								this.mescroll.endErr()
							}
						})
					}
				} else {
					if (this.isVisitMode) {
						getOtherUserPostList(this.hostUserId, pageIndex, pageSize).then(res => {
							res.data.list.forEach(item => {
								if (item.image_list && item.image_list.length > 0) {
									item.icon = 'pic'
								}
								item.show_time = getTimeDistance(item.created_at)
							})
							let pageList = res.data.list
							if (pageIndex == 0) {
								this.dataList = pageList
							} else {
								this.dataList = this.dataList.concat(pageList);
							}
							console.log('page length:' + pageList.length)
							let hasNext = pageList.length >= pageSize
							if (this.mescroll) {
								this.mescroll.endSuccess(pageList.length, hasNext);
							}
						}).catch(error => {
							if (this.mescroll) {
								this.mescroll.endErr()
							}
						})
					} else {
						getUserPostList(pageIndex, pageSize).then(res => {
							res.data.list.forEach(item => {
								if (item.image_list && item.image_list.length > 0) {
									item.icon = 'pic'
								}
								item.show_time = getTimeDistance(item.created_at)
							})
							let pageList = res.data.list
							if (pageIndex == 0) {
								this.dataList = pageList
							} else {
								this.dataList = this.dataList.concat(pageList);
							}
							console.log('page length:' + pageList.length)
							let hasNext = pageList.length >= pageSize
							if (this.mescroll) {
								this.mescroll.endSuccess(pageList.length, hasNext);
							}
						}).catch(error => {
							if (this.mescroll) {
								this.mescroll.endErr()
							}
						})
					}
				}
			},
			downCallback() {
				if (this.mescroll) {
					this.mescroll.resetUpScroll()
				}
			},
			upCallback(page) {
				let pageIndex = page.num - 1
				this.getDataList(pageIndex, page.size)
			},
		}
	}
</script>

<style scoped>
	.header {
		padding: 80rpx 90rpx 60rpx 90rpx;
		box-sizing: border-box;
	}

	.title {
		font-size: 34rpx;
		color: #333;
		font-weight: 500;
	}

	.sub-title {
		font-size: 24rpx;
		color: #7a7a7a;
		padding-top: 18rpx;
	}

	.tui-primary {
		color: #5677FC;
	}
</style>
